package com.example.appmanager.user;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.appmanager.Appconfig.Appconfig;
import com.example.appmanager.Appconfig.ToastShow;
import com.example.appmanager.R;
import com.example.appmanager.entity.ReaponseMsg;
import com.example.appmanager.entity.UserTableEntity;
import com.example.appmanager.services.AllresposeService;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class UserRegister extends AppCompatActivity {
    private EditText userRealName;//用户的真实姓名
    private EditText userAccount;//用户的账号
    private EditText userPwd;//用户密码
    private EditText surePwd;//二次校验密码
    private EditText userAge;//用户的年龄
    private RadioGroup radioGroup;//用户性别
    private Button button;//用户注册按钮
    private UserTableEntity userEntity = new UserTableEntity();//
    private ToastShow toastShow = new ToastShow();
    private ImageView image_back_login;//返回登录
    private Toast toast;
    private boolean getData = false;
    private String userJson = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_user);
        initUi();//绑定界面空间的id
        setListenerClik();//调用事件监听器
    }
    /*用户提交注册按钮
     * */
    private void setListenerClik() {

        /*用户注册按钮*/
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*这里应该还有一个对全部的判空*/
                if (checkInfo()) {//检查用户填写注册的信息是否为空   不为空就进入
                    /*这里是判断两次密码是否一样*/
                    if ((userPwd.getText().toString().trim()).equals(surePwd.getText().toString().trim())) {
                        if (setData()) {//给用户实体进行赋值
                            Log.d("userRegister", userEntity.toString());
                            /*进行网络调用*///Log.d("userRegister", String.valueOf(getDataResponse(userEntity)));
                            if (!getDataResponse(userJson)) {//把实体上传到网络进行注册
                                toast = toastShow.getToast(getApplicationContext(), "注册失败！", 1000);
                                toast.show();
                            } else {
                                toast = toastShow.getToast(getApplicationContext(), "注册成功！", 1000);
                                toast.show();
                            }
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "两次密码不一样", Toast.LENGTH_LONG).show();
                    }
                }
                return;
                /*else {
                    Toast.makeText(getApplicationContext(), "信息不能为空！请认真填写。", Toast.LENGTH_LONG).show();
                }*/
            }
        });
        /*用户返回登录图片标签*/
        image_back_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),Login.class);
                startActivity(intent);
            }
        });
    }
    /*上传数据 进行用户的注册功能！！*/
    private boolean getDataResponse(String user) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Appconfig.BASE_URL)
                .client(Appconfig.CLIENT) //这是设置请求的时间等信息 连接时间  上传等时间
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        AllresposeService request = retrofit.create(AllresposeService.class);
        Call<ReaponseMsg> userEntityCall = request.createUser(user);
        userEntityCall.enqueue(new Callback<ReaponseMsg>() {
            @Override
            public void onResponse(Call<ReaponseMsg> call, Response<ReaponseMsg> response) {
                /*
                 * */
                int id = response.body().getBug();
                Log.d("userRegister",String.valueOf(id));
                if (id==1){
                    getData = true;
                }

            }
            @Override
            public void onFailure(Call<ReaponseMsg> call, Throwable t) {
                getData = false;
                Log.d("reasponseMsg",String.valueOf(t.getMessage()));
                System.exit(0);
            }
        });
        return getData;
    }
    /*初始化控件*/
    private void initUi() {
        userRealName = findViewById(R.id.editTextUname);//用户的真实姓名
        userAccount = findViewById(R.id.editAccount);//用户的账号
        userPwd = findViewById(R.id.editText_paswd);//用户密码
        surePwd = findViewById(R.id.register_sure_pwd);//二次校验密码
        userAge = findViewById(R.id.user_age); //用户的年龄
        radioGroup = findViewById(R.id.userSex);//用户性别
        button = findViewById(R.id.button_other_register);//用户注册按钮
        image_back_login = findViewById(R.id.back_register);//用户返回登录界面
    }
    /*
    初始化数据   在调用该方法的时候记得判断两次输入的密码一样
    * */
    private boolean setData() {

        if (userAge.getText().toString().isEmpty()) {
            userEntity.setAge(18);
        } else {
            userEntity.setAge(Integer.parseInt(userAge.getText().toString().trim()));//设置用户的年龄
        }
        int count = radioGroup.getChildCount();
        for (int i = 0; i < count; i++) {
            RadioButton rb = (RadioButton) radioGroup.getChildAt(i);
            if (rb.isChecked()) {
                Log.d("userRegister", rb.getText().toString());
                userEntity.setSex(rb.getText().toString());//设置性别
                break;
            }
        }
        userEntity.setUaccount(userAccount.getText().toString().trim());//设置用户的账户
        userEntity.setUname(userRealName.getText().toString().trim());//设置用户的真实姓名
        userEntity.setUpassword(userPwd.getText().toString().trim());//设置用户的密码
        JSON json = (JSON) JSONArray.toJSON(userEntity);
        userJson = String.valueOf(json);
        Log.d("userRegister1", userJson);
        if (userEntity != null) {
            return true;
        }

        return false;
    }
    /*判断用户输入的信息是否是空的*/
    private boolean checkInfo() {
       /* userRealName = findViewById(R.id.editTextUname);//用户的真实姓名
        userAccount = findViewById(R.id.editTextAccount);//用户的账号
        userPwd = findViewById(R.id.editText_paswd);//用户密码
        surePwd = findViewById(R.id.register_sure_pwd);//二次校验密码*/
        if (userRealName.getText().toString().isEmpty()) {
            Toast.makeText(getApplicationContext(), "用户姓名不能为空！", Toast.LENGTH_LONG).show();
            return false;
        } else if ((userAccount.getText().toString().isEmpty())) {
            Toast.makeText(getApplicationContext(), "用户的账户不能为空！", Toast.LENGTH_LONG).show();
            return false;
        } else if (userPwd.getText().toString().isEmpty()) {
            Toast.makeText(getApplicationContext(), "用户密码不能为空！", Toast.LENGTH_LONG).show();
            return false;
        } else if (surePwd.getText().toString().isEmpty()) {
            Toast.makeText(getApplicationContext(), "两次输入的密码不一致！", Toast.LENGTH_LONG).show();
            return false;
        }else if ((userAccount.getText().length()>10)) {
            Toast.makeText(getApplicationContext(), "账号过长！(10长度)", Toast.LENGTH_LONG).show();
            return false;
        }//
        return true;
    }
}
